home *** CD-ROM | disk | FTP | other *** search
- /* Revision History:
-
- 93/12/24 aih
- - added a function to test if a key is down
-
- 93/03/26 AIH
- - Changed KeyEdit to KeyToCmd and added checks for the help key
-
- 92/02/20 AIH
- - Added ByteLib instead of using ugly bit masks
- - Added menu commands instead menu IDs and menu item numbers
-
- 91/05/15 AIH
- - Added function to test for arrow keys
-
- 91/03/24 AIH
- - Added support for international canceling
-
- 91/03/07 AIH
- - Standard command keys aren't read from a resource because it really
- makes some of the code messy (can't use a switch statement on variables)
- and because these keys really are standard
-
- 91/01/05 Ari Halberstadt (AIH)
- - Inserted this standard header in all files */
-
- #include <limits.h>
- #include <Script.h>
- #include "ByteLib.h"
- #include "KeyLib.h"
-
- /* return the virtual key code of the event message */
- unsigned char KeyVirtual(EventRecord *event)
- {
- unsigned char key = 0;
-
- if (event->what == keyDown || event->what == autoKey)
- key = HiByte(LoWord(event->message));
- return(key);
- }
-
- /* Return the equivalent standard edit command for a key down event. First,
- a check is made for command key equivalents of the standard edit menu
- commands, then a check is made for use of the extended keyboard function
- keys. A check is also made for the clear key. The appropriate constant
- corresponding to the standard edit menu item is returned, or CMD_NONE if the
- key doesn't correspond to a command. */
- MenuCommandType KeyToCmd(EventRecord *event)
- {
- unsigned char key = event->message;
- MenuCommandType cmd = CMD_NONE;
-
- if (event->what == keyDown || event->what == autoKey) {
- /* check for keyboard equivalents of commands */
- if ((event->modifiers & cmdKey) == 0) {
- switch (KeyVirtual(event)) {
- case undoVFKey: cmd = CMD_UNDO; break;
- case cutVFKey: cmd = CMD_CUT; break;
- case copyVFKey: cmd = CMD_COPY; break;
- case pasteVFKey: cmd = CMD_PASTE;break;
- case helpVKey: cmd = CMD_HELP; break;
- default: if (key == clearKey) cmd = CMD_CLEAR; break;
- }
- }
- else {
- switch (key) {
- case undoKey: cmd = CMD_UNDO; break;
- case cutKey: cmd = CMD_CUT; break;
- case copyKey: cmd = CMD_COPY; break;
- case pasteKey: cmd = CMD_PASTE;break;
- case clearKey: cmd = CMD_CLEAR;break;
- }
- }
- }
- return(cmd);
- }
-
- /* True if the event specifies a command-period combination or the escape
- key. Adapted from TN#263 "International Canceling". */
- Boolean KeyCancel(EventRecord *event)
- {
-
- unsigned char virtual; /* virtual key from event message */
- short code; /* character from event message */
- long info; /* result of KeyTrans */
- long state; /* state for KeyTrans */
- Handle kchr; /* the 'KCHR' resource */
- Boolean result = false;
-
- if (event->what == keyDown || event->what == autoKey) {
-
- /* get the virtual key code */
- virtual = HiByte(LoWord(event->message));
-
- /* check for command-period */
- if ((event->modifiers & cmdKey) != 0) {
-
- /* load the current 'KCHR' resource */
- info = event->message;
- kchr = GetResource('KCHR', GetScript(GetEnvirons(smKeyScript), smScriptKeys));
- if (kchr && *kchr) {
-
- /* build a key code for KeyTrans */
- code = ((HiByte(LoWord(event->modifiers)) & ~cmdKey) | virtual);
- state = 0;
-
- /* no need to lock resource since KeyTrans won't move memory */
- info = KeyTrans(*kchr, code, &state);
- }
-
- /* examine result */
- result = (LoByte(info) == cancelKey ||
- LoByte(HiWord(info)) == cancelKey);
- }
- else
- result = (virtual == escapeVKey);
- }
- return(result);
- }
-
- /* true if the key is down */
- Boolean KeyIsDown(unsigned char key)
- {
- KeyMap map;
-
- GetKeys(map);
- return((((char *) map)[key / 8] & (1 << (key % 8))) != 0);
- }
-
- /* true if an arrow key */
- Boolean KeyIsArrow(unsigned char key)
- {
- switch(key) {
- case arrowLeftKey:
- case arrowRightKey:
- case arrowUpKey:
- case arrowDownKey:
- return(true);
- }
- return(false);
- }
-
- /* true if event specifies an arrow key */
- Boolean KeyArrow(EventRecord *event)
- {
- Boolean result = false;
-
- if (event->what == keyDown || event->what == autoKey)
- result = KeyIsArrow(event->message);
- return(result);
- }
-
- /* true if event specifies a movement key */
- Boolean KeyMovement(EventRecord *event)
- {
- unsigned char key = event->message;
- Boolean result = KeyArrow(event);
-
- if (! result) {
- switch(KeyVirtual(event)) {
- case pageUpVKey:
- case pageDownVKey:
- case homeVKey:
- case endVKey:
- result = true;
- break;
- }
- }
- return(result);
- }
-
- /* true if event specifies a command key */
- Boolean KeyCmd(EventRecord *event)
- {
- unsigned char key = event->message;
- Boolean result = false;
-
- if (event->what == keyDown || event->what == autoKey) {
- result = ((event->modifiers & cmdKey) != 0 ||
- KeyToCmd(event) != CMD_NONE ||
- KeyCancel(event) ||
- KeyMovement(event) ||
- key == backspaceKey);
- }
- return(result);
- }
-
- /* true if event specifies a dialog command key */
- Boolean KeyCmdDlg(EventRecord *event)
- {
- return(KeyCmd(event));
- }
-
- /* true if event specifies a modal dialog command key */
- Boolean KeyCmdDlgModal(EventRecord *event)
- {
- unsigned char key = event->message;
- Boolean result = false;
-
- if (event->what == keyDown || event->what == autoKey)
- result = (KeyCmdDlg(event) || key == enterKey || key == returnKey);
- return(result);
- }
-